// 导入express框架
const express=require('express')
// 创建express实例
const app=express()

var bodyParser = require('body-parser')

// 导入cors
const cors=require('cors')
// 全局挂载
app.use(cors())

// multer是一个nodej中间件,用于处理multipart/form-data类型的表单数据,它主要用于上传文件
const multer=require("multer")
// 在server服务端下新建一个public文件,在public文件下新建upload文件用于存放图片
const upload=multer({dest:'./public/upload'})
app.use(upload.any())
// 静态托管
app.use(express.static("./public"))


// parse application/x-www-form-urlencoded
// 当extended为false,值为数组或者字符串,当为true时,值可以为任意类型
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use((req,res,next)=>{
	// status=0为成功,=1为失败,默认设为1,方便处理失败的情况
	res.cc=(err,status=1)=>{
		res.send({
			status,
			message:err instanceof Error ? err.message:err
		})
	}
	next()
})

const jwtconfig=require('./jwt_config/index.js')
const {expressjwt:jwt}=require('express-jwt')

// app.use(jwt({
// 	secret:jwtconfig.jwtSecretKey,algorithms:['HS256'],
// }).unless({
// 	path:[/^\/api\//]
// }))

const loginRouter=require('./router/login.js')
app.use('/api',loginRouter)

const userRouter=require('./router/userinfo.js')
app.use('/user',userRouter)

const setRouter=require('./router/setting.js')
app.use('/set',setRouter)

const productRouter = require('./router/product.js')
app.use('/pro', productRouter)

const messageRouter = require('./router/message.js')
app.use('/msg', messageRouter)

const fileRouter = require('./router/file.js')
app.use('/file', fileRouter)

const loginLogRouter = require('./router/login_log.js')
app.use('/llog', loginLogRouter)

const operationLogRouter = require('./router/operation_log.js')
app.use('/olog', operationLogRouter)

const overviewLogRouter = require('./router/overview.js')
app.use('/ov', overviewLogRouter)

const depMsgRouter = require('./router/department_msg.js')
app.use('/dm', depMsgRouter)

const Joi = require('joi')



// 对不符合joi规则的情况进行报错
app.use((err,req,res,next)=>{
	if(err instanceof Joi.ValidationError) return res.cc(err)
})

// 绑定和侦听指定的主机和端口
app.listen(3007, () => {
	console.log('http://127.0.0.1:3007')
})